package com.yzx.lifeassistants.model.impl;

import java.util.List;
import java.util.Properties;

import android.content.Context;

import com.yzx.lifeassistants.bean.OfficeInform;
import com.yzx.lifeassistants.model.IQueryOfficeInform;
import com.yzx.lifeassistants.model.analysis.QueryOfficeInformAnalysis;
import com.yzx.lifeassistants.model.callback.IQueryOfficeInformCB;
import com.yzx.lifeassistants.utils.HttpClientUtil;
import com.yzx.lifeassistants.utils.IHttpCallBack;
import com.yzx.lifeassistants.utils.LogcatUtils;
import com.yzx.lifeassistants.utils.PropertyUtil;

/**
 * @Description: 查询教务处通知
 * @author: yzx
 * @time: 2016-3-10 下午10:06:25
 */
public class QueryOfficeInformImpl implements IQueryOfficeInform {

	public static final String TAG = "QueryOfficeInformImpl";
	private IQueryOfficeInformCB queryCallBack;// 回调
	private String url;// URL

	public QueryOfficeInformImpl(Context context,
			IQueryOfficeInformCB queryCallBack) {
		this.queryCallBack = queryCallBack;
		Properties properties = PropertyUtil.getProperties(context,
				"base.properties");
		url = properties.getProperty("query_office_inform_url");
	}

	/**
	 * 
	 * @Description: 查询教务处通知
	 */
	@Override
	public void queryOfficeInform(int page) {
		HttpClientUtil.sendRequest(url + "?page=" + page, null,
				new IHttpCallBack() {

					@Override
					public void onFinish(String response) {
						LogcatUtils.i(response);
						boolean isNoMore = QueryOfficeInformAnalysis
								.isNoMore(response);
						if (isNoMore) {// 暂无更多
							LogcatUtils.e("暂无更多教务处通知 ");
							queryCallBack
									.queryFailed(IQueryOfficeInformCB.NO_MORE);
						} else {
							List<OfficeInform> informs = QueryOfficeInformAnalysis
									.getData(response);
							if (null == informs || 1 > informs.size()) {
								LogcatUtils.e("暂无更多教务处通知 ");
								queryCallBack
										.queryFailed(IQueryOfficeInformCB.NO_MORE);
							} else {
								LogcatUtils.e("查询教务处通知成功 ");
								queryCallBack.querySuccess(informs);
							}
						}
					}

					@Override
					public void onError(Exception e) {
						LogcatUtils.e("获取数据失败");
						if (null != e.getMessage()) {
							LogcatUtils.e(e.getMessage());
						}
						queryCallBack
								.queryFailed(IQueryOfficeInformCB.QUERY_FAILED);
					}
				});
	}

}
